//
//  LargeGoodsTableViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/8/14.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "LargeGoodsTableViewController.h"
#import "LargeGoodsCell.h"
#import "OrderPayViewController.h"
#import "LargeGoodsDetailsViewController.h"

@interface LargeGoodsTableViewController ()

@property (nonatomic, strong) NSArray *jsonArray;

@end

static NSString *CellIdentifier = @"LargeGoodsCell";

@implementation LargeGoodsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"LargeGoodsCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
//    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadBulkorder) name:@"reloadBulkorder" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)reloadBulkorder {
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_queue_t queue = dispatch_queue_create("gcdtest.rongfzh.yc", DISPATCH_QUEUE_CONCURRENT);
        dispatch_barrier_async(queue, ^{
            [self requestData];
        });
    }];
    [self.tableView.header beginRefreshing];
}

- (void)requestData {
    NSString *string = @"";
    if (self.largeGoodsType == WaittingReceive) {
        string = @"1";
    } else if (self.largeGoodsType == AlreadyReceive) {
        string = @"2";
    } else {
        string = @"3";
    }
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken, @"type":string};
    [RequestData bulkorderListParameters:[UIUtil toJsonString:dic] withBlock:^(NSArray *jsonArray, NSString *msg, NSError *error) {
        if (error) {
            [self.tableView.header endRefreshing];
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
        } else {
            [[ESHUDView sharedInstance] showSuccessMessage:@"请求大货单列表成功" dismissBlock:nil];
            [self.tableView.header endRefreshing];
            self.jsonArray = jsonArray;
            [self.tableView reloadData];
        }
    }];
}

- (void)goToPay:(BulkorderModel *)model {
    OrderPayViewController *view = [[OrderPayViewController alloc] init];
    view.toPayId = model.toPayId;
    [[UIUtil viewControllerHasNavgation:self.view].navigationController pushViewController:view animated:YES];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.jsonArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LargeGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[LargeGoodsCell alloc] initWithStyle:UITableViewCellStyleDefault
                                     reuseIdentifier:CellIdentifier];
    }
    cell.bulkorderModel = [self.jsonArray objectAtIndex:indexPath.row];
    if (self.largeGoodsType == WaittingPaying) {
        cell.ifWaittingPay = YES;
        __weak __typeof(&*self)weakSelf = self;
        cell.largeGoodsBlock = ^(){
            [weakSelf goToPay:[weakSelf.jsonArray objectAtIndex:indexPath.row]];
        };
    } else {
        cell.ifWaittingPay = NO;
    }
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LargeGoodsDetailsViewController *view = [[LargeGoodsDetailsViewController alloc] init];
    view.bulkorderModel = [self.jsonArray objectAtIndex:indexPath.row];
    [[UIUtil viewControllerHasNavgation:self.view].navigationController pushViewController:view animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 94;
}


@end
